
//
// 20120401 - AH - (Garage) Created.
// 20120728 - AH - Responds with array not list as used to do.
// 20120825 - AH - Now serializable RatbasesSerializable. 
//

package org.soekrat.server.servlets.file;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.soekrat.server.io.db.Folder;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;
import org.soekrat.server.serialization.RatbaseSerializable;
import org.soekrat.server.serialization.RatbasesSerializable;
import org.soekrat.server.servlets.AbstractSoekratHttpServlet;

/**
 * <p>
 *   Implements request to get names and id of all rat bases.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class GetSoekratRatBases extends AbstractSoekratHttpServlet
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public GetSoekratRatBases(){}
  
  @Override
  protected void handleRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException 
  {
    SoekratDAO dao=new SoekratDAOFactory().create();
    
    List<Folder> ratRoots=dao.getFolders(-1, "Rat Bases");
    if (ratRoots.isEmpty())
    {
      respond(res, "no root for rat bases found");
      return;
    }
    
    Folder ratRoot=ratRoots.get(0);
    List<Folder> ratBases=dao.getFolders(ratRoot.id(), null);
    
    RatbasesSerializable bases=new RatbasesSerializable();
    
    for (Folder curr : ratBases)
      bases.add(new RatbaseSerializable(curr.id(), curr.name()));
    
    respond(res, bases);
  }
  
}
